﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.DAL;
using HRMSys.Model;

namespace HRMSys.UI
{
    /// <summary>
    /// PrintSalarySheet.xaml 的交互逻辑
    /// </summary>
    public partial class PrintSalarySheet : Window
    {
        public PrintSalarySheet()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cboDepartment.ItemsSource = (new DepartmentDAL()).GetAll(true);
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            int year = Convert.ToInt32(txtYear.Text);
            int month = Convert.ToInt32(txtMonth.Text);
            Guid departId = (Guid)cboDepartment.SelectedValue;

            if ((new SalarySheetDAL()).IsExists(year, month, departId) == false)
            {
                MessageBox.Show("该部门还未生成工资单");
            }
            else 
            {
                SalarySheetItem[] items = (new SalarySheetDAL()).GetSalarySheetItems(year, month, departId);
                SalarySheetItemRPT[] rptItems = new SalarySheetItemRPT[items.Length];
                for (int i = 0; i < items.Length; i++)
                {
                    SalarySheetItemRPT rptItem = new SalarySheetItemRPT();
                    rptItem.EmployeeName = ((new EmployeeDAL()).GetById(items[i].EmployeeId)).Name;
                    rptItem.BaseSalary = items[i].BaseSalary;
                    rptItem.Bonus = items[i].Bonus;
                    rptItem.Fine = items[i].Fine;
                    rptItem.Other = items[i].Other;
                    rptItems[i] = rptItem;
                }
                SalaryReport report = new SalaryReport();
                report.SetDataSource(rptItems);
                report.SetParameterValue("年", year);
                report.SetParameterValue("月", month);
                report.SetParameterValue("部门", (new DepartmentDAL()).GetById(departId).DepartName);

                reportViewer.ViewerCore.ReportSource = report;
            }
        }
    }
}
